import request from '../utils/request.js';

/**
 * 封装掘金请求方法
 * @param {*} url 请求地址
 * @param {*} type 请求类型
 * @param {*} data 请求数据
 * @returns
 */
function juejinRequest(url, type = 'POST', data = {}) {
  const headers = {
    cookie: document.cookie,
  };
  return request(url, {
    method: type,
    headers,
    data,
  });
}

// 查询是否有免费抽奖次数
export const freeLuckDrawSecond = async () => {
  const url = 'https://api.juejin.cn/growth_api/v1/lottery_config/get';
  return await juejinRequest(url, 'GET');
};

// 进行免费抽奖
export const freeLuckDraw = async () => {
  const url = 'https://api.juejin.cn/growth_api/v1/lottery/draw';
  return await juejinRequest(url);
};

// 获取用户信息
export const getUserInfo = async () => {
  const url = 'https://api.juejin.cn/user_api/v1/user/get';
  return await juejinRequest(url, 'GET');
};

// 查询用户是否签到
export const querySignin = async () => {
  const url = 'https://api.juejin.cn/growth_api/v1/get_today_status';
  return await juejinRequest(url, 'GET');
};

// 进行签到
export const singnin = async () => {
  const url = 'https://api.juejin.cn/growth_api/v1/check_in';
  return await juejinRequest(url);
};

// 沾福气
export const getLucky = async uuid => {
  const url = `https://api.juejin.cn/growth_api/v1/lottery_lucky/dip_lucky?aid=2608&uuid=${uuid}`;
  const data = {
    lottery_history_id: '7057551468475187203',
  };
  const result = await juejinRequest(url, 'POST', data);
  if (result.data.has_dip) {
    console.log('掘金~今日已沾福');
  } else {
    console.log('掘金~沾福气成功');
  }
  if (result.err_no !== 0) return alert('沾福失败，请检查是否已登录掘金~');
};
